﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Common;
using XF.Meeting.Entity.CommonEntity;
using XF.Meeting.Entity.CommonEntity.wx;
using XF.Meeting.Entity.Enum;
using XF.Meeting.Entity.View;
using XF.Meeting.IDal;
using XF.Sugar.IService;
namespace XF.Meeting.Service
{
    public class BPMService : BaseService,IBPMService
    {
        IBPMDal dal = null;
        public BPMService(string Operator, string OperaIP) {
            this.Operator = Operator;
            this.OperaIP = OperaIP;
            if (dal == null)
            {
                lock (this.OperaIP)
                {
                    if (dal == null)
                    {
                        dal = DalFactory.DalFactory.GetBPMDal();
                    }
                }
            }
        }
        public Task<string> ApproveClose(string strBTID, string strBOID, int iProcInstID, string eProcessInstanceResult, string strComment, DateTime dtTime)
        {
            int meetState = 1;
            int summaryState = 2;
            switch (eProcessInstanceResult)
            {
                case "1":
                    meetState = (int)MeetingEnum.order;
                    summaryState = (int)SummaryEnum.end;
                    break;
                case "0":
                    meetState = (int)MeetingEnum.nopass;
                    summaryState = (int)SummaryEnum.nopass;
                    break;
                case "2":
                    meetState = (int)MeetingEnum.nullify;
                    summaryState = (int)SummaryEnum.nullify;
                    break;
                default:
                    break;
            }
            var result = dal.ApproveClose( strBTID,  strBOID,  iProcInstID,  eProcessInstanceResult,  strComment,  dtTime,meetState,summaryState);
            if (result.Result)
            {
                return Task.FromResult("<success>成功</success>");
            }
            else
            {
                return Task.FromResult("<error>失败</error>");
            }
        }

        public Task<string> Audit(string strBTID, string strBOID, int iProcInstID, string strStepName, string strApproverId, string eAction, string strComment, DateTime dtTime)
        {
            int meetState = 1;
            int summaryState = 2;
            switch (eAction)
            {
                //case "1":
                //    meetState = (int)MeetingEnum.order;
                //    summaryState = (int)SummaryEnum.end;
                //    break;
                case "2":
                    meetState = (int)MeetingEnum.nopass;
                    summaryState = (int)SummaryEnum.nopass;
                    break;
            }

            var result = dal.Audit( strBTID,  strBOID,iProcInstID,  strStepName,  strApproverId,  eAction,  strComment,  dtTime,meetState,summaryState);
            if (result.Result)
            {
                return Task.FromResult("<success>成功</success>");
            }
            else {
                return Task.FromResult("<error>失败</error>");
            }
        }

        public Task<string> CreateResult(string strBTID, string strBOID, int iProcInstID, string bSuccess, string procURL, string strMessage)
        {
            var result = dal.CreateResult( strBTID,  strBOID,  iProcInstID, bSuccess, procURL, strMessage);
            if (result.Result)
            {
                return Task.FromResult("<success>成功</success>");
            }
            else
            {
                return Task.FromResult("<error>失败</error>");
            }
        }

        public Task<string> DoInvalid(string bsid, string btid, string boid, string procInstID, string userid, string invalidComment)
        {
            var result = dal.DoInvalid( bsid,  btid,  boid,  procInstID,  userid,  invalidComment);
            if (result.Result)
            {
                return Task.FromResult("<success>成功</success>");
            }
            else
            {
                return Task.FromResult("<error>失败</error>");
            }
        }
        /// <summary>
        /// 获取会议信息或纪要信息
        /// </summary>
        /// <param name="CInfoId">会议id或者纪要id</param>
        /// <param name="type">1 会议 2纪要</param>
        /// <returns></returns>
        public async Task<BPMConfig> GetBPMCONFIG(int CInfoId,int type)
        {
            var result = await dal.GetBPMCONFIG(CInfoId,type,out string s);

          
            if (s == "")
            {
                BPMConfig bPM = TableToList.ToList<BPMConfig>(result.Tables[0]).FirstOrDefault();
                

                
                var data = TableToList.ToList<ProcessXmlData>(result.Tables[1]).FirstOrDefault();
                bPM.Guid = result.Tables[1].Rows[0]["Guid"].ToString();
                TimeSpan A = data.EndTime - data.BeginTime;
                var da = new DATA()
                {
                    title = data.title,
                    MtApprove = data.MtApprove,
                    MtDuration = A.TotalMinutes.ToString(),
                    MtLeader = data.MtLeader,
                    MtReporter = data.MtReporter,
                    MtSubject = data.MtSubject,
                    MtTime = data.BeginTime.ToString("g") + "--" + data.EndTime.ToString("g"),
                     RoomName=data.RoomName,
                      procInstID=data.procInstID
                };

                if (type == 1)
                {
                    var res = dal.GetCInfoByID(CInfoId).Result;
                    var list = TableToList.ToList<MeetingInfoSingle>(res).FirstOrDefault();
                    bPM.ProcessData = da;
                    var men = TableToList.ToList<PersonInfo>(result.Tables[2]);
                    if (men != null && men.Count > 0)
                    {
                        var str = "";
                        foreach (var item in men)
                        {
                            str += item.AccountName + ",";
                        }
                        bPM.ProcessData.MtParticipant = str.Remove(str.Length - 1, 1);
                    }
                    if (string.IsNullOrWhiteSpace(list.CRoomIDs))
                    {
                        bPM.ProcessData.RoomName = list.CRoom;
                    }
                    else {
                        bPM.ProcessData.RoomName = list.CRoom + "," + list.CRooms;
                    }
                }
                else {
                    var res = dal.GetCInfoByMinutesID(CInfoId).Result;
                    var list = TableToList.ToList<MeetingInfoSingle>(res).FirstOrDefault();
                    bPM.SummaryData = da;
                    var men = TableToList.ToList<PersonInfo>(result.Tables[2]);
                
                  
              
                    if (men != null && men.Count > 0)
                    {
                        var str = "";
                        foreach (var item in men)
                        {
                            str += item.AccountName + ",";
                        }
                      
                        bPM.SummaryData.MtParticipant = str.Remove(str.Length - 1, 1);
                    }
                    bPM.SummaryData.MtAttachment = TableToList.ToList<MtAttachment>(result.Tables[3]);
                    if (string.IsNullOrWhiteSpace(bPM.SummaryData.RoomName))
                    {
                        if (string.IsNullOrWhiteSpace(list.CRoomIDs))
                        {
                            bPM.SummaryData.RoomName = list.CRoom;
                        }
                        else
                        {
                            bPM.SummaryData.RoomName = list.CRoom + "," + list.CRooms;
                        }
                    }
                    
                }

                return bPM;
            }
            else {
                return new BPMConfig();
            }
            
        
        }
        public async Task<WxConfig> GetWXCONFIG() {
            var result = await dal.GetWXCONFIG( out string s);
            var config = TableToList.ToList<WxConfig>(result).FirstOrDefault();
            return config;
        }

        public Task<bool> UpdateBusinessData(string strBTID, string strBOID, int iProcInstID, string strStepName, string strApproverId, string strXML, DateTime dtTime)
        {
            var result = dal.UpdateBusinessData( strBTID,  strBOID,iProcInstID,  strStepName,  strApproverId,  strXML,dtTime);
            return result;
        }

        public Task<bool> WriteBusinessDataToBPM(string bsid, string btid, string boid, string bsxml, string procInstID, string userid, string processUrl,int proType,int Id)
        {
            var result = dal.WriteBusinessDataToBPM( bsid,  btid,  boid,  bsxml, procInstID, userid,processUrl, proType,Id);
            return result;
        }
    }
}

